import { ProColumns } from '@/components/Pro';

export function getHiddenColumnKeysFromRecords<T extends Record<string, unknown>>(
  records: T[],
): string[] {
  const hiddenColumnKeys: string[] = [];
  const keyCountMap: Record<string, number> = {};
  // 判断条件，某字段在所有record中都为空
  records.forEach((record) => {
    Object.keys(record).forEach((key) => {
      if (record[key] === null || record[key] === undefined) {
        if (keyCountMap[key]) {
          keyCountMap[key]++;
        } else {
          keyCountMap[key] = 1;
        }
      }
    });
  });

  Object.keys(keyCountMap).forEach((key) => {
    if (keyCountMap[key] === records.length) {
      hiddenColumnKeys.push(key);
    }
  });

  return hiddenColumnKeys;
}

export function hideEmptyDataColumns(columns: ProColumns[], hiddenColumnKeys: string[]) {
  return columns.map((column) => {
    if (hiddenColumnKeys.includes(column.dataIndex as string)) {
      return {
        ...column,
        hideInSearch: true,
        hideInTable: true,
      };
    } else {
      return column;
    }
  });
}
